package com.gzq.service.impl;

import java.util.List;

import com.gzq.dao.FavouriteDao;
import com.gzq.dao.HospitalDao;
import com.gzq.entity.Favourite;
import com.gzq.entity.Hospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gzq.service.HospitalService;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalDao hospitalDao;
    @Autowired
    private FavouriteDao favouriteDao;

    @Override
    public List<Hospital> findHosByRe() {

        return hospitalDao.findHosByRe();
    }

    @Override
    public Hospital findHosById(int id) {

        return hospitalDao.findHosById(id);
    }

    @Override
    public int findOrderHosNum(String province, String city, String district, Hospital hospital) {

        if (hospital.getHospitalNature() != null && hospital.getHospitalNature().equals("默认")) {
            hospital.setHospitalNature(null);
        }
        if (hospital.getHospitalGrade() != null && hospital.getHospitalGrade().equals("默认")) {
            hospital.setHospitalGrade(null);
        }
        if (province != null && province.equals("默认")) {
            province = "";
        }
        if (city != null && city.equals("默认")) {
            city = "";
        }
        if (district != null && district.equals("默认")) {
            district = "";
        }

        if (province.equals("") && city.equals("") && district.equals("")) {

        } else if (!province.equals("") && city.equals("") && district.equals("")) {
            hospital.setHospitalAddress(province);
        } else if (!province.equals("") && !city.equals("") && district.equals("")) {
            hospital.setHospitalAddress(city);
        } else if (!province.equals("") && !city.equals("") && !district.equals("")) {
            hospital.setHospitalArea(district);
        }
        return hospitalDao.findOrderHosNum(hospital.getHospitalName(), hospital.getIsMedicalInsurance(),
                hospital.getHospitalGrade(), hospital.getHospitalNature(), hospital.getHospitalAddress(),
                hospital.getHospitalArea());
    }

    @Override
    public List<Hospital> findfindOrderHos(int start, int size) {

        return hospitalDao.findOrderHos(start, size);
    }

    @Override
    public List<String> findHosOpenGrade() {

        return hospitalDao.findHosOpenGrade();
    }

    @Override
    public List<String> findHosGrade() {

        return hospitalDao.findHosGrade();
    }

    @Override
    public List<String> findHosOpenNature() {

        return hospitalDao.findHosOpenNature();
    }

    @Override
    public List<String> findHosNature() {

        return hospitalDao.findHosNature();
    }

    @Override
    public List<Hospital> findHosByConditon(String province, String city, String district, Hospital hospital, int start,
                                            int size) {
        if (hospital.getHospitalNature() != null && hospital.getHospitalNature().equals("默认")) {
            hospital.setHospitalNature(null);
        }
        if (hospital.getHospitalGrade() != null && hospital.getHospitalGrade().equals("默认")) {
            hospital.setHospitalGrade(null);
        }
        if (province != null && province.equals("默认")) {
            province = "";
        }
        if (city != null && city.equals("默认")) {
            city = "";
        }
        if (district != null && district.equals("默认")) {
            district = "";
        }

        if (province.equals("") && city.equals("") && district.equals("")) {

        } else if (!province.equals("") && city.equals("") && district.equals("")) {
            hospital.setHospitalAddress(province);
        } else if (!province.equals("") && !city.equals("") && district.equals("")) {
            hospital.setHospitalAddress(city);
        } else if (!province.equals("") && !city.equals("") && !district.equals("")) {
            hospital.setHospitalArea(district);
        }

        return hospitalDao.findHosByCondition(hospital.getHospitalName(), hospital.getIsMedicalInsurance(),
                hospital.getHospitalGrade(), hospital.getHospitalNature(), hospital.getHospitalAddress(),
                hospital.getHospitalArea(), start, size);
    }

    @Override
    public Hospital findHosByName(String hosptalName) {

        return hospitalDao.findHosByName(hosptalName);
    }

    @Override
    public List<Hospital> findOpenHos() {

        return hospitalDao.findOpenHos();
    }

    @Override
    public int findAllHosNum(String province, String city, String district, Hospital hospital) {

        if (hospital.getHospitalNature() != null && hospital.getHospitalNature().equals("默认")) {
            hospital.setHospitalNature(null);
        }
        if (hospital.getHospitalGrade() != null && hospital.getHospitalGrade().equals("默认")) {
            hospital.setHospitalGrade(null);
        }
        if (province != null && province.equals("默认")) {
            province = "";
        }
        if (city != null && city.equals("默认")) {
            city = "";
        }
        if (district != null && district.equals("默认")) {
            district = "";
        }

        if (province.equals("") && city.equals("") && district.equals("")) {

        } else if (!province.equals("") && city.equals("") && district.equals("")) {
            hospital.setHospitalAddress(province);
        } else if (!province.equals("") && !city.equals("") && district.equals("")) {
            hospital.setHospitalAddress(city);
        } else if (!province.equals("") && !city.equals("") && !district.equals("")) {
            hospital.setHospitalArea(district);
        }
        return hospitalDao.findAllHosNum(hospital.getHospitalName(), hospital.getIsMedicalInsurance(),
                hospital.getHospitalGrade(), hospital.getHospitalNature(), hospital.getHospitalAddress(),
                hospital.getHospitalArea());
    }

    @Override
    public List<Hospital> findAllHosByConditon(String province, String city, String district, Hospital hospital,
                                               int start, int size) {
        if (hospital.getHospitalNature() != null && hospital.getHospitalNature().equals("默认")) {
            hospital.setHospitalNature(null);
        }
        if (hospital.getHospitalGrade() != null && hospital.getHospitalGrade().equals("默认")) {
            hospital.setHospitalGrade(null);
        }
        if (province != null && province.equals("默认")) {
            province = "";
        }
        if (city != null && city.equals("默认")) {
            city = "";
        }
        if (district != null && district.equals("默认")) {
            district = "";
        }

        if (province.equals("") && city.equals("") && district.equals("")) {

        } else if (!province.equals("") && city.equals("") && district.equals("")) {
            hospital.setHospitalAddress(province);
        } else if (!province.equals("") && !city.equals("") && district.equals("")) {
            hospital.setHospitalAddress(city);
        } else if (!province.equals("") && !city.equals("") && !district.equals("")) {
            hospital.setHospitalArea(district);
        }

        return hospitalDao.findAllHosByCondition(hospital.getHospitalName(), hospital.getIsMedicalInsurance(),
                hospital.getHospitalGrade(), hospital.getHospitalNature(), hospital.getHospitalAddress(),
                hospital.getHospitalArea(), start, size);
    }

    /**
     * 是否关注医院
     * 如果第一次关注医院先插入记录，如果不是第一次，进行isLike的更改
     */
    @Override
    public int favourite(int userId, int hospitalId) {

        if (favouriteDao.findFavByuserIdAndHosId(userId, hospitalId) == null) {
            Favourite favourite = new Favourite();
            favourite.setUserId(userId);
            favourite.setHospitalId(hospitalId);
            //默认isLIke == 0
            favouriteDao.insetFavourite(favourite);
        }
        Favourite favouriteInfo = favouriteDao.findFavByuserIdAndHosId(userId, hospitalId);
        int isLike = favouriteInfo.getIsLike();
        //如果isLIke == 0,则点击的是关注，设置isLike==1
        if (isLike == 0) {
            favouriteDao.addFavourite(favouriteInfo.getId());
            isLike = 1;
        } else {
            //如果isLIke == 1,则点击的是关注，设置isLike==0
            favouriteDao.cancelFavourite(favouriteInfo.getId());
            isLike = 0;
        }
        return isLike;
    }

    @Override
    public List<Hospital> findFavHos(List<Favourite> favourites) {

        return hospitalDao.findFavHos(favourites);
    }

}
